home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Snippets / Append 1.0.2 / Append.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-01  |  3.2 KB  |  147 lines  |  [TEXT/CWIE]

  1. /*
  2.  
  3. append.c -- originally by Mark^Zimmermann
  4.             ported to CW by Ken Long
  5.             updated for CW6 on 950711
  6.             updated for CW7 on 951201
  7.  
  8. This snippet lets you append chosen text files together. Just pick
  9. the first file, and then subsequent choices from the StdFile dialog
  10. are tacked onto the end of it.
  11.  
  12. */
  13.  
  14. // prototypes
  15.  
  16. void    main(void);
  17. void    AppendFile(short refNum0, Str255 *fnXp, short vRefX);
  18. int        GetTheFiles(Str255 *fnp, short *vRefp);
  19. void    pStrCopy(char *p1, char *p2);
  20. void    GiveUp(void);
  21.  
  22. //• set up theBufferfer of size 25,600 bytes (= 512 * 50) for copying through;
  23. //• ktheBufferferSize must be an integer, < 32768
  24.  
  25. #define ktheBufferferSize  25600
  26.  
  27. char    theBuffer[ktheBufferferSize];
  28.  
  29. //• set up a little window to give info to the user...very static
  30.  
  31. WindowRecord myWinRecord;
  32. WindowPtr infoWindow;
  33. Str255 myString;
  34.  
  35. //• main routine to initialize everything under the sun (is all this
  36. //• really necessary?) and then get the names of the files to join ... if
  37. //• they open ok, start joining them, and quit when the user chooses
  38. //• "Cancel"....
  39.  
  40. void main() 
  41. {
  42.     Str255 fn0, fnX;
  43.     short vRef0, vRefX, refNum0, i;
  44.  
  45.     InitGraf (&qd.thePort);
  46.     InitFonts ();
  47.     FlushEvents (everyEvent, 0);
  48.     InitWindows ();
  49.     InitMenus ();
  50.     TEInit ();
  51.     InitDialogs (0L);
  52.     InitCursor ();
  53.     MaxApplZone ();
  54.     
  55.     infoWindow = GetNewWindow (128, &myWinRecord, (WindowPtr) - 1L);
  56.     ShowWindow (infoWindow);
  57.     SetPort (infoWindow);
  58.     TextFont (0);
  59.     for (i = 1; i <= 3; ++i)
  60.     {
  61.         MoveTo (4, 15 * i - 5);
  62.         GetIndString ((StringPtr) myString, 128, i);
  63.         DrawString ((StringPtr) myString);
  64.     }
  65.  
  66.     if (GetTheFiles (&fn0, &vRef0))
  67.     {
  68.         if (FSOpen ((StringPtr) fn0, vRef0, &refNum0) != noErr)
  69.             GiveUp ();
  70.         while (GetTheFiles (&fnX, &vRefX))
  71.             AppendFile (refNum0, &fnX, vRefX);
  72.         FSClose (refNum0);
  73.     }
  74. }
  75.  
  76. //• Routine to append the contents of file X to file 0.
  77.  
  78. void AppendFile(short refNum0, Str255 *fnXp, short vRefX)
  79. {
  80.     short refNumX, err;
  81.     long count;
  82.     
  83.     if (FSOpen ((StringPtr) fnXp, vRefX, &refNumX) != noErr)
  84.         GiveUp ();
  85.     if (SetFPos (refNumX, fsFromStart, 0L) != noErr)
  86.         GiveUp ();
  87.     if (SetFPos (refNum0, fsFromLEOF, 0L) != noErr)
  88.         GiveUp ();
  89.     for (;;)
  90.     {
  91.         count = ktheBufferferSize;
  92.         err = FSRead (refNumX, &count, theBuffer);
  93.         if (err != noErr && err != eofErr)
  94.             GiveUp ();
  95.         if (count == 0)
  96.             break;
  97.         if (FSWrite (refNum0, &count, theBuffer) != noErr)
  98.             GiveUp ();
  99.     }
  100.     FSClose (refNumX);
  101.     return;
  102. }
  103.  
  104. //• following variables and routine do the standard files dialog
  105. //• to get the name of the file use ... cribbed from the MiniEdit
  106. //• example that comes with LSC....
  107.  
  108. static Point SFGwhere = { 90, 82 };
  109. static SFReply reply;
  110.  
  111. int GetTheFiles (Str255 *fnp, short *vRefp)
  112. {
  113.     SFTypeList myTypes;
  114.  
  115.     myTypes[0]='TEXT';
  116.     SFGetFile( SFGwhere, "\p", 0L, 1, myTypes, 0L, &reply);
  117.     if (reply.good)
  118.     {
  119.         pStrCopy( (char *)reply.fName, (char *)fnp);
  120.         *vRefp = reply.vRefNum;
  121.         return(1);
  122.     }
  123.     else return(0);
  124. }
  125.  
  126. //• routine to copy a pascal myString from one place to another.... used in
  127. //• above Standard Files routine....
  128.  
  129. void pStrCopy(char *p1, char *p2)
  130. {
  131.     short len;
  132.     
  133.     len = *p2++ = *p1++;
  134.     while (--len >= 0)
  135.         *p2++ = *p1++;
  136.     return;
  137. }
  138.  
  139. //• routine to give up with a beep if an error occurs during opening the
  140. //• file or fixing it....
  141.  
  142. void GiveUp ()
  143. {
  144.     SysBeep (10);
  145.     ExitToShell ();
  146. }
  147.